set more off

*cd "DIRECTORY HERE"

use "DataSet_ReadyforAnalysis.dta", clear

***Sets Panel
xtset fip_code year


**Sets Global Treatment
global treatments t_shooting t_fatalshooting t_non_fatalshooting

***Descriptive Statistics

sum t_shooting t_fatalshooting TotalVotes  RepVotesTotalPercent DemVotesTotalPercent turnout population_total population_non_white_percentage population_agetovote_percentage  change_unem_rate if TotalVotes !=.


***Label Vars and Descriptions

label var t_shooting "Any Shooting (t-1)" // Dummy variable =1 if there was a shooting in county between previous elections and this election. 


label var t_non_fatalshooting "Non-fatal Shooting (t-1)" // Dummy variable =1 if there was a non fatal shooting in county between previous elections and this election. otherwise. 
label var t_fatalshooting "Fatal Shooting (t-1)" // Dummy variable =1 if there was a fatal shooting in county between previous elections and this election.  


label var shooting_before_midterm "Before Midterm Election" // Dummy variable =1 if there was a shooting between previous elections and this election and the shooting occurred before midterm election. 
label var shooting_after_midterm "After Midterm Election" // Dummy variable =1 if there was a shooting between previous elections and this election and the shooting occurred after midterm election . 


label var sd_distance_closest_shooting "Distance (Std)" // Distance between county and closest county with a shooting measures in standard deviations (miles).  


label var between "Days Between Shooting and Elections" // Number of days between shooting and election 



***Covariate Balance Test
{
preserve 

keep fip_code fip_state t_shooting t_fatalshooting TotalVotes RepVotes DemVotes RepVotesTotalPercent DemVotesTotalPercent population_total population_non_white_percentage population_agetovote_percentage turnout change_unem_rate

keep if TotalVotes!=.

sum t_shooting t_fatalshooting TotalVotes RepVotes DemVotes RepVotesTotalPercent DemVotesTotalPercent population_total population_non_white_percentage population_agetovote_percentage turnout population_non_white_percentage change_unem_rate

destring fip_state, gen(state_n)

logit t_shooting population_total population_non_white_percentage population_agetovote_percentage change_unem_rate i.state_n

predict pscore, pr

gen iptw = cond(t_shooting, 1/pscore, 1/(1-pscore))

***Covariate Balance
covbal t_shooting population_total population_agetovote_percentage population_agetovote_percentage change_unem_rate, wt(iptw)

restore

}


****Figure 2
{
preserve

collapse (sum) shooting n_deaths, by(year)
 
twoway bar shooting year, yaxis(1) || line n_deaths year, yaxis(2)
 
restore
}



***Main models with turnout as Outcome

	foreach x of global treatments{
	xtreg  turnout `x'  i.year if (population_total!=. & prop_non_white!=. & change_unem_rate!=.) , fe cluster(fip_state) robust
	estimates store m1_`x'

	xtreg  turnout `x'   population_total prop_non_white change_unem_rate i.year, fe robust cluster(fip_state)
	estimates store m2_`x'
	
	xtreg  turnout `x'  i.year if neighbor==1 | ever_shooting==1, fe robust cluster(fip_state)
	estimates store m1_`x'_rest

	xtreg  turnout `x'   population_total prop_non_white change_unem_rate i.year if neighbor==1 | ever_shooting==1, fe robust cluster(fip_state)
	estimates store m2_`x'_rest
	}
	
**FIGURE 3
coefplot (m1_t_shooting m1_t_fatalshooting m1_t_non_fatalshooting, label(County-Time FE)) (m2_t_shooting m2_t_fatalshooting m2_t_non_fatalshooting, label(County-Time FE + Controls)) (m1_t_shooting_rest m1_t_fatalshooting_rest m1_t_non_fatalshooting_rest, label(County-Time FE - Neighbors)) (m2_t_shooting_rest m2_t_fatalshooting_rest m2_t_non_fatalshooting_rest, label(County-Time FE + Controls - Neighbors)), keep(t_fatalshooting t_shooting t_non_fatalshooting) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 0 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Turnout, in percentage points")



***Main models with DemVoteShare as Outcome
	foreach x of global treatments{
	xtreg  DemVotesTotalPercent `x' i.year if (population_total!=. & prop_non_white!=. & change_unem_rate!=.) , cluster(fip_state) fe robust
	estimates store m1_`x'

	xtreg  DemVotesTotalPercent `x'   population_total prop_non_white change_unem_rate i.year, fe cluster(fip_state) robust 
	estimates store m2_`x'
	
	xtreg  DemVotesTotalPercent `x'  i.year if neighbor==1 | ever_shooting==1, fe cluster(fip_state) robust
	estimates store m1_`x'_rest

	xtreg  DemVotesTotalPercent `x'  population_total prop_non_white change_unem_rate i.year if neighbor==1 | ever_shooting==1, fe  cluster(fip_state) robust 
	estimates store m2_`x'_rest
	}
	
**FIGURE 4
coefplot (m1_t_shooting m1_t_fatalshooting m1_t_non_fatalshooting, label(County-Time FE)) (m2_t_shooting m2_t_fatalshooting m2_t_non_fatalshooting, label(County-Time FE + Controls)) (m1_t_shooting_rest m1_t_fatalshooting_rest m1_t_non_fatalshooting_rest, label(County-Time FE - Neighbors)) (m2_t_shooting_rest m2_t_fatalshooting_rest m2_t_non_fatalshooting_rest, label(County-Time FE + Controls - Neighbors)), keep(t_fatalshooting t_shooting t_non_fatalshooting) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Democratic Vote Share, in percentage points")


***HETEROGENEOUS TREATMENT EFFECTS 

****Only on swing states

*Dem Vote Share as Outcome
{
foreach x of global treatments{
	xtreg  DemVotesTotalPercent `x' i.year if (swing_state==1 & population_total!=. & prop_non_white!=. & change_unem_rate!=.), fe robust cluster(fip_state)
	estimates store m1_`x'


	xtreg  DemVotesTotalPercent `x' population_total prop_non_white change_unem_rate i.year if (swing_state==1), fe robust  cluster(fip_state)
	estimates store m2_`x' 
	
	xtreg  DemVotesTotalPercent `x' i.year if  swing_state==1 & (neighbor==1 | ever_shooting==1), fe robust cluster(fip_state)
	estimates store m1_`x'_rest 

	xtreg  DemVotesTotalPercent `x' population_total prop_non_white change_unem_rate i.year if  swing_state==1 & (neighbor==1 | ever_shooting==1), fe robust cluster(fip_state)
	estimates store m2_`x'_rest
}
	
*FIGURE 5
coefplot (m1_t_shooting m1_t_fatalshooting m1_t_non_fatalshooting, label(County-Time FE)) (m2_t_shooting m2_t_fatalshooting m2_t_non_fatalshooting, label(County-Time FE + Controls)) (m1_t_shooting_rest m1_t_fatalshooting_rest m1_t_non_fatalshooting_rest, label(County-Time FE - Neighbors)) (m2_t_shooting_rest m2_t_fatalshooting_rest m2_t_non_fatalshooting_rest, label(County-Time FE + Controls - Neighbors)), keep(t_fatalshooting t_shooting t_non_fatalshooting) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Democratic Vote Share, in percentage points")


}



*Turnout as Outcomeforeach x of global treatments
{
	xtreg  turnout `x' i.year if (swing_state==1 & population_total!=. & prop_non_white!=. & change_unem_rate!=.), fe robust cluster(fip_state)
	estimates store m1_`x'
	outreg2 using "FINAL RESULTS/regs_swing.xls", append pvalue


	xtreg  turnout `x' population_total prop_non_white change_unem_rate i.year if (swing_state==1), fe robust  cluster(fip_state)
	estimates store m2_`x' 
	outreg2 using "FINAL RESULTS/regs_swing.xls", append pvalue
	
	xtreg  turnout `x' i.year if  swing_state==1 & (neighbor==1 | ever_shooting==1), fe robust cluster(fip_state)
	estimates store m1_`x'_rest 
	outreg2 using "FINAL RESULTS/regs_swing.xls", append pvalue

	xtreg  turnout `x' population_total prop_non_white change_unem_rate i.year if  swing_state==1 & (neighbor==1 | ever_shooting==1), fe robust cluster(fip_state)
	estimates store m2_`x'_rest
	outreg2 using "FINAL RESULTS/regs_swing.xls", append pvalue

	}
*FIGURE 5 (turnout not in paper)
coefplot (m1_t_shooting m1_t_fatalshooting m1_t_non_fatalshooting, label(County-Time FE)) (m2_t_shooting m2_t_fatalshooting m2_t_non_fatalshooting, label(County-Time FE + Controls)) (m1_t_shooting_rest m1_t_fatalshooting_rest m1_t_non_fatalshooting_rest, label(County-Time FE - Neighbors)) (m2_t_shooting_rest m2_t_fatalshooting_rest m2_t_non_fatalshooting_rest, label(County-Time FE + Controls - Neighbors)), keep(t_fatalshooting t_shooting t_non_fatalshooting) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Turnout, in percentage points")


******HTE: TIME ELECTIONS
{
xtreg  DemVotesTotalPercent i.t_shooting##i.year, cluster(fip_state) fe robust

margins year, at(t_shooting==0) at(t_shooting==1) 

*FIGURE 6
marginsplot, ytitle("Predicted Democratic Vote Share (%)") title(" ") 


xtreg  turnout i.t_shooting##i.year, cluster(fip_state) fe robust

margins year, at(t_shooting==0) at(t_shooting==1) 

*FIGURE 7
marginsplot, ytitle("Predicted Turnout (%)") title(" ")
}



******Heterogeneus treatment effects by how strict gun laws are
{
reg  DemVotesTotalPercent i.t_shooting##i.state_bans i.year, robust cluster(fip_state)	

margins state_bans, at(t_shooting==0 ) at(t_shooting==1 ) 

*Figure 8
marginsplot, ytitle("Predicted Democratic Vote Share (%)") title(" ") xtitle("From less (F) to more strict (A)")

reg turnout i.t_shooting##i.state_bans i.year, robust cluster(fip_state)		

margins state_bans, at(t_shooting==0 ) at(t_shooting==1 ) 

marginsplot, ytitle("Predicted Turnout (%)") title(" ") xtitle("From less (F) to more strict (A)")
}


****Appendix: Variations of main models for robustness

****1. State fixed effects
 foreach x of global treatments{
	*Outcome: Democratic Vote Share
	reg  DemVotesTotalPercent `x'  i.year i.fip_state2 if (population_total!=. & prop_non_white!=. & unem_rate!=.), robust cluster(fip_state)
	estimates store m1_`x'

	reg  DemVotesTotalPercent `x' population_total prop_non_white unem_rate i.year i.fip_state2, robust  cluster(fip_state)
	estimates store m2_`x'
	
	*Restricted
	reg  DemVotesTotalPercent `x'  i.year i.fip_state2 if neighbor==1 | ever_shooting==1, robust cluster(fip_state)
	estimates store m1_`x'_rest

	reg  DemVotesTotalPercent `x' population_total prop_non_white unem_rate i.year i.fip_state2 if neighbor==1 | ever_shooting==1, robust cluster(fip_state)
	estimates store m2_`x'_rest	
	}
	
coefplot (m1_t_shooting m1_t_fatalshooting m1_t_non_fatalshooting, label(State and Time FE)) (m2_t_shooting m2_t_fatalshooting m2_t_non_fatalshooting, label(State and Time FE + Controls)) (m1_t_shooting_rest m1_t_fatalshooting_rest m1_t_non_fatalshooting_rest, label(State and Time FE - Neighbors)) (m2_t_shooting_rest m2_t_fatalshooting_rest m2_t_non_fatalshooting_rest, label(State and Time FE + Controls Neighbors)), keep(t_fatalshooting t_shooting t_non_fatalshooting) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Percentage Points")


	*Outcome: Turnout
	foreach x of global treatments{
	reg  turnout `x'  i.year i.fip_state2 if (population_total!=. & prop_non_white!=. & unem_rate!=.), robust cluster(fip_state)
	estimates store m1_`x'

	reg  turnout `x' population_total prop_non_white unem_rate i.year i.fip_state2, robust  cluster(fip_state)
	estimates store m2_`x'
	
	*Restricted
	reg  turnout `x'  i.year i.fip_state2 if neighbor==1 | ever_shooting==1, robust cluster(fip_state)
	estimates store m1_`x'_rest

	reg  turnout `x' population_total prop_non_white unem_rate i.year i.fip_state2 if neighbor==1 | ever_shooting==1, robust cluster(fip_state)
	estimates store m2_`x'_rest	
	}
	
	
coefplot (m1_t_shooting m1_t_fatalshooting m1_t_non_fatalshooting, label(State and Time FE)) (m2_t_shooting m2_t_fatalshooting m2_t_non_fatalshooting, label(State and Time FE + Controls)) (m1_t_shooting_rest m1_t_fatalshooting_rest m1_t_non_fatalshooting_rest, label(State and Time FE - Neighbors)) (m2_t_shooting_rest m2_t_fatalshooting_rest m2_t_non_fatalshooting_rest, label(State and Time FE + Controls Neighbors)), keep(t_fatalshooting t_shooting t_non_fatalshooting) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Percentage Points")



****2.INCLUDING COUNTY AND DECADE FIXED EFFECTS
***Outcome: DemVoteShare
	foreach x of global treatments{
	xtreg  DemVotesTotalPercent `x' i.decade if (population_total!=. & prop_non_white!=. & change_unem_rate!=.) , cluster(fip_state) fe robust
	estimates store m1_`x'

	xtreg  DemVotesTotalPercent `x'   population_total prop_non_white change_unem_rate i.decade, fe cluster(fip_state) robust 
	estimates store m2_`x'
	
	xtreg  DemVotesTotalPercent `x'  i.decade if neighbor==1 | ever_shooting==1, fe cluster(fip_state) robust
	estimates store m1_`x'_rest

	xtreg  DemVotesTotalPercent `x'  population_total prop_non_white change_unem_rate i.decade if neighbor==1 | ever_shooting==1, fe  cluster(fip_state) robust 
	estimates store m2_`x'_rest
	}
	

coefplot (m1_t_shooting m1_t_fatalshooting m1_t_non_fatalshooting, label(County-Decade FE)) (m2_t_shooting m2_t_fatalshooting m2_t_non_fatalshooting, label(County-Decade FE + Controls)) (m1_t_shooting_rest m1_t_fatalshooting_rest m1_t_non_fatalshooting_rest, label(County-Decade FE Neighbors)) (m2_t_shooting_rest m2_t_fatalshooting_rest m2_t_non_fatalshooting_rest, label(County-Decade FE +Controls Neighbors)), keep(t_fatalshooting t_shooting t_non_fatalshooting) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Democratic Vote Share, in percentage points")

****Outcome: Turnout
	foreach x of global treatments{
	xtreg  turnout `x'  i.decade if (population_total!=. & prop_non_white!=. & change_unem_rate!=.) , fe cluster(fip_state) robust
	estimates store m1_`x'

	xtreg  turnout `x'   population_total prop_non_white change_unem_rate i.decade, fe robust cluster(fip_state)
	estimates store m2_`x'
	
	xtreg  turnout `x'  i.decade if neighbor==1 | ever_shooting==1, fe robust cluster(fip_state)
	estimates store m1_`x'_rest

	xtreg  turnout `x'   population_total prop_non_white change_unem_rate i.decade if neighbor==1 | ever_shooting==1, fe robust cluster(fip_state)
	estimates store m2_`x'_rest
	}
	
coefplot (m1_t_shooting m1_t_fatalshooting m1_t_non_fatalshooting, label(County-Decade FE)) (m2_t_shooting m2_t_fatalshooting m2_t_non_fatalshooting, label(County-Decade FE + Controls)) (m1_t_shooting_rest m1_t_fatalshooting_rest m1_t_non_fatalshooting_rest, label(County-Decade FE Neighbors)) (m2_t_shooting_rest m2_t_fatalshooting_rest m2_t_non_fatalshooting_rest, label(County-Decade FE + Controls Neighbors)), keep(t_fatalshooting t_shooting t_non_fatalshooting) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 0 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Turnout, in percentage points")


****3.Including home state advantage variables as controls
*Turnout as Outcome
	foreach x of global treatments{
	xtreg  turnout `x' home_state_dem home_state_rep i.year if (population_total!=. & prop_non_white!=. & change_unem_rate!=.) , cluster(fip_state) fe robust
	estimates store m1_`x'

	xtreg  turnout `x'  home_state_dem home_state_rep population_total prop_non_white change_unem_rate i.year, fe cluster(fip_state) robust 
	estimates store m2_`x'
	
	xtreg  turnout `x' home_state_dem home_state_rep i.year if neighbor==1 | ever_shooting==1, fe cluster(fip_state) robust
	estimates store m1_`x'_rest

	xtreg  turnout `x' home_state_dem home_state_rep population_total prop_non_white change_unem_rate i.year if neighbor==1 | ever_shooting==1, fe  cluster(fip_state) robust 
	estimates store m2_`x'_rest
	}
	

coefplot (m1_t_shooting m1_t_fatalshooting m1_t_non_fatalshooting, label(County and Time FE)) (m2_t_shooting m2_t_fatalshooting m2_t_non_fatalshooting, label(County and Time FE + Controls)) (m1_t_shooting_rest m1_t_fatalshooting_rest m1_t_non_fatalshooting_rest, label(County and Time FE - Neighbors)) (m2_t_shooting_rest m2_t_fatalshooting_rest m2_t_non_fatalshooting_rest, label(County and Time FE + Controls - Neighbors)), keep(t_fatalshooting t_shooting t_non_fatalshooting) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Percentage Points")


*Dem Vote Share as Outcome
	foreach x of global treatments{
	xtreg  DemVotesTotalPercent `x' home_state_dem home_state_rep i.year if (	population_total!=. & prop_non_white!=. & change_unem_rate!=.) , cluster(fip_state) fe robust
	estimates store m1_`x'

	xtreg  DemVotesTotalPercent `x'  home_state_dem home_state_rep population_total prop_non_white change_unem_rate i.year, fe cluster(fip_state) robust 
	estimates store m2_`x'
	
	xtreg  DemVotesTotalPercent `x' home_state_dem home_state_rep i.year if neighbor==1 | ever_shooting==1, fe cluster(fip_state) robust
	estimates store m1_`x'_rest

	xtreg  DemVotesTotalPercent `x' home_state_dem home_state_rep population_total prop_non_white change_unem_rate i.year if neighbor==1 | ever_shooting==1, fe  cluster(fip_state) robust 
	estimates store m2_`x'_rest
	}
	
coefplot (m1_t_shooting m1_t_fatalshooting m1_t_non_fatalshooting, label(County and Time FE)) (m2_t_shooting m2_t_fatalshooting m2_t_non_fatalshooting, label(County and Time FE + Controls)) (m1_t_shooting_rest m1_t_fatalshooting_rest m1_t_non_fatalshooting_rest, label(County and Time FE - Neighbors)) (m2_t_shooting_rest m2_t_fatalshooting_rest m2_t_non_fatalshooting_rest, label(County and Time FE + Controls - Neighbors)), keep(t_fatalshooting t_shooting t_non_fatalshooting) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Percentage Points")


****4. Mass Shootings
{
xtreg  DemVotesTotalPercent mass_shooting_past4years i.year, cluster(fip_state) fe robust
estimates store m1

xtreg  turnout mass_shooting_past4years i.year, cluster(fip_state) fe robust
estimates store m2


label var mass_shooting_past4years "Mass Shooting"
coefplot  m2 || m1, keep(mass_shooting_past4years) xline(0) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Percentage Points")

}



****5.DISTANCE AS OPPOSED TO SHOOTING AS IV
{
	*Distance in Miles
	*Democratic Vote Share
	xtreg  DemVotesTotalPercent sd_distance_closest_shooting i.year, cluster(fip_state) fe robust
	estimates store m1_distance
	
	*Turn Out
	xtreg  turnout sd_distance_closest_shooting i.year, cluster(fip_state) fe robust
	estimates store m1_distance_turnout

	*Plot Figure code
	coefplot (m1_distance) || (m1_distance_turnout)  , keep(sd_distance_closest_shooting) 		mlabel xlabel(-5 0 5) xline(0) format(%8.3g) mlabposition(12) mlabgap(*2) xtitle(		"Percentage Points")

}


****6. Interaction of competitive election
{
global outcomes1 turnout DemVotesTotalPercent
set more off
foreach x of global treatments{
	foreach y of global outcomes1{

	xtreg  `y' `x'##i.competitive_election i.year if (population_total!=. & prop_non_white!=. & unem_rate!=.), fe robust 

	xtreg  `y' `x'##i.competitive_election population_total prop_non_white unem_rate i.year, fe robust 
	
	xtreg  `y' `x'##i.competitive_election i.year if neighbor==1 | ever_shooting==1, fe robust

	xtreg  `y' `x'##i.competitive_election population_total prop_non_white unem_rate i.year if neighbor==1 | ever_shooting==1, fe robust 
	}
	
}
}


****7. Weighted by how close it is
****Days to Election
{
reg DemVotesTotalPercent between, cluster(fip_state) robust
estimates store m1

reg turnout between, cluster(fip_state) robust
estimates store m2


coefplot m1 || m2, keep(between) xline(0) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Percentage Points") xlabel(-0.01 0 0.01)
}


****Before After Midterm Elections
{
label var t_shooting "Shooting (t - 1) | Before Midterm Election"
label var interaction_midterm "After Midterm Interaction"

**Dem vote share as Outcome
xtreg  DemVotesTotalPercent t_shooting interaction_midterm shooting_after_midterm  i.year if (population_total!=. & prop_non_white!=. & change_unem_rate!=.), fe cluster(fip_state) robust
estimates store m1

xtreg  DemVotesTotalPercent t_shooting interaction_midterm shooting_after_midterm population_total  prop_non_white change_unem_rate i.year if (population_total!=. & prop_non_white!=. & change_unem_rate!=.), fe cluster(fip_state) robust
estimates store m2

coefplot (m1, label(FE)) (m2, label(FE + controls)), keep(t_shooting interaction) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Percentage Points")


**Turnout as Outcome
xtreg  turnout t_shooting interaction_midterm shooting_after_midterm i.year if (population_total!=. & prop_non_white!=. & unem_rate!=.), fe cluster(fip_state) robust
estimates store m1_to

xtreg  turnout t_shooting interaction_midterm shooting_after_midterm population_total  prop_non_white unem_rate i.year if (population_total!=. & prop_non_white!=. & unem_rate!=.), fe cluster(fip_state) robust
estimates store m2_to


**Figure before after midterm - turnout 
coefplot (m1_to, label(FE)) (m2_to, label(FE + controls)), keep(t_shooting interaction) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Percentage Points")

}

label var t_shooting "Any Shooting (t-1)"

************ ADDITIONAL ANALYSES

************Placebo tests with LEADS
***** 
{
label var flead_all  "Any Shooting Lead (t+1)"
label var flead_fatal "Fatal Shooting Lead (t+1)"
label var flead_nonfatal "Non-Fatal Shooting Lead (t+1)"


global leads flead_all flead_fatal flead_nonfatal


set more off
foreach x of global leads{
xtreg  DemVotesTotalPercent `x'  i.year if (population_total!=. & prop_non_white!=. & change_unem_rate!=.) , cluster(fip_state) fe robust
	estimates store m1_`x'
	outreg2 using "FINAL RESULTS/Main1_placebo.xls", append pvalue

	xtreg  DemVotesTotalPercent `x' population_total prop_non_white change_unem_rate i.year, fe cluster(fip_state) robust 
	estimates store m2_`x'
	outreg2 using "Nov 24/Main1_placebo.xls", append pvalue
	
} 

coefplot (m1_flead_all m1_flead_fatal m1_flead_nonfatal, label(County and Time FE)) (m2_flead_all m2_flead_fatal m1_flead_nonfatal, label(County and Time FE + Controls)), keep(flead_all flead_fatal flead_nonfatal) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Democratic Vote Share, in percentage points")


foreach x of global leads{
xtreg  turnout `x'  i.year if (population_total!=. & prop_non_white!=. & change_unem_rate!=.) , cluster(fip_state) fe robust
	estimates store m1_`x'

	xtreg  turnout `x' population_total prop_non_white change_unem_rate i.year, fe cluster(fip_state) robust 
	estimates store m2_`x'
	
} 

coefplot (m1_flead_all m1_flead_fatal m1_flead_nonfatal, label(County and Time FE)) (m2_flead_all m2_flead_fatal m1_flead_nonfatal, label(County and Time FE + Controls)), keep(flead_all flead_fatal flead_nonfatal) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Turnout, in percentage points")

}



***********PARALLEL TRENDS GRAPHS
global years 1984 1988 1992 1996 2000 2004 2008 2012 2016
foreach x of global years{

preserve
drop if n_elections_treated>=2
collapse (mean) RepVotesTotalPercent DemVotesTotalPercent, by(year treatment_`x'_f) 

reshape wide DemVotesTotalPercent RepVotesTotalPercent, i(year) j(treatment_`x'_f)

local b = `x' - 3

gen c=100 if year>=`x' - 4 & year<=`x'

label var DemVotesTotalPercent0 "Counties without Shooting"

label var DemVotesTotalPercent1 "Counties with Shooting"

label var RepVotesTotalPercent0 "Counties without Shooting"

label var RepVotesTotalPercent1 "Counties with Shooting"

label var c "Period of Shooting"

graph twoway (area c year, color(gs8%30)) (line DemVotesTotalPercent0 DemVotesTotalPercent1 year, lstyle(solid dash) lcolor(gs0 gs10)  sort), title("Shooting between `b' and `x' ") plotregion(fcolor(white)) graphregion( fcolor(white)) legend(off)

restore 
}

***Randomization Inference
{
xtset fip_code year
ritest t_shooting _b[t_shooting] _se[t_shooting], strata(year) rep(1000) : ///
areg DemVotesTotalPercent t_shooting i.year if (population_total!=. & prop_non_white!=. & change_unem_rate!=.), absorb(fip_code)

ritest t_shooting _b[t_shooting] _se[t_shooting], strata(year) rep(1000) : ///
areg DemVotesTotalPercent t_shooting population_total prop_non_white change_unem_rate i.year if (population_total!=. & prop_non_white!=. & unem_rate!=.), absorb(fip_code)

}


**********DMA AS UNIT OF ANALYSIS

set more off

cd "/Users/lauragarciamontoya/Dropbox/Shootings and voting in the U.S./6. Analysis/RegressionsOutput"

use "DataSet_ReadyforAnalysis.dta", clear

drop if dmaindex==.

collapse (max) t_shooting t_fatalshooting t_non_fatalshooting (sum)  population_total     (mean) DemVotesTotalPercent turnout change_unem_rate prop_non_white, by(dmaindex year)

global treatments t_shooting t_fatalshooting t_non_fatalshooting


xtset dmaindex year

***DemVoteShare as Outcome
	foreach x of global treatments{
	xtreg  DemVotesTotalPercent `x' i.year if (population_total!=. & prop_non_white!=. & change_unem_rate!=.) , cluster(dmaindex) fe robust
	estimates store m1_`x'

	xtreg  DemVotesTotalPercent `x' population_total prop_non_white change_unem_rate i.year, fe cluster(dmaindex) robust 
	estimates store m2_`x'
	}
	
coefplot (m1_t_shooting m1_t_fatalshooting m1_t_non_fatalshooting, label(DMA and Time FE)) (m2_t_shooting m2_t_fatalshooting m2_t_non_fatalshooting, label(DMA and Time FE + Controls)), keep(t_fatalshooting t_shooting t_non_fatalshooting) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Democratic Vote Share, in percentage points")


***turnout as Outcome
	foreach x of global treatments{
	xtreg  turnout `x' i.year if (population_total!=. & prop_non_white!=. & change_unem_rate!=.) , cluster(dmaindex) fe robust
	estimates store m1_`x'

	xtreg  turnout `x' population_total prop_non_white change_unem_rate i.year, fe cluster(dmaindex) robust 
	estimates store m2_`x'
	
	}
		
coefplot (m1_t_shooting m1_t_fatalshooting m1_t_non_fatalshooting, label(DMA and Time FE)) (m2_t_shooting m2_t_fatalshooting m2_t_non_fatalshooting, label(DMA and Time FE + Controls)), keep(t_fatalshooting t_shooting t_non_fatalshooting) xline(0) xsc(r(-0.1 0.05)) xlabel(-10 -5 0 5 10) mlabel format(%8.3g) mlabposition(12) mlabgap(*2) xtitle("Turnout, in percentage points")



********TWO WAY FE WEIGHTS and MULTIPLE GROUP AND TIME TREATMENTS
**We use package twowayfeweights package developed by Chaisemartin & D'Haultfoeuille (2020a) to estimate the DID_M which takes into account heterogeneous treatment effects over time or across groups. "It estimates the ATE across all the (g,t) celles whose treatment changes fron t-1 to t. "

use "DataSet_ReadyforAnalysis.dta", clear

keep DemVotesTotalPercent fip_state turnout fip_code year t_shooting population_total prop_non_white change_unem_rate


*Without Controls
twowayfeweights DemVotesTotalPercent fip_code year t_shooting, type(feTR)
twowayfeweights turnout fip_code year t_shooting, type(feTR)


*With Controls
twowayfeweights DemVotesTotalPercent fip_code year t_shooting, type(feTR) controls(population_total prop_non_white change_unem_rate)

twowayfeweights turnout fip_code year t_shooting, type(feTR) controls(population_total prop_non_white change_unem_rate)


**MODEL ESTIMATIONS

***WITHOUT CONTROLS
did_multiplegt DemVotesTotalPercent fip_code year t_shooting, breps(100)  seed(200) 
did_multiplegt turnout fip_code year t_shooting, breps(100)  seed(200)


did_multiplegt DemVotesTotalPercent fip_code year t_shooting, breps(100) controls(population_total prop_non_white change_unem_rate) seed(200)


did_multiplegt turnout fip_code year t_shooting, breps(100) controls(population_total prop_non_white change_unem_rate) seed(200)














